#include "authorizationsrv.h"
#include "../AN/an.client.h"
#include "../USS/USSclient.h"

// Constructor
TOB::AuthorizationSRV::AuthorizationSRV(TOB::Nameserver::IP nsip, 
    std::string username, std::string password): 
    AuthRequired(nsip, username, password) {
  AN::Client an(nsip, username, password);
  res = an.online(online);
}


// Check policy
Code::Type TOB::AuthorizationSRV::policy(Policy::Type policy, bool &out) {
  if(res != Code::Okay) { return res; }
  if(online) {
    US us(nsip);
    Code::Type code;
    Policy::Type p;
    int32 temp;
    if((code = us.getUintByLogin(username, "policy", temp)) != Code::Okay)
      { return code; }
    p = static_cast<Policy::Type>(temp);
    out = policy & p;
  } else {
    out = false;
  }
  return Code::Okay; 
}


// Check role
Code::Type TOB::AuthorizationSRV::role(Role::Type role, bool &out) {
  if(res != Code::Okay) { return res; }
  if(online) {
    US us(nsip);
    Code::Type code;
    int32 temp;
    if((code = us.getUintByLogin(username, "role", temp)) != Code::Okay)
      { return code; }
    Role::Type r;
    r = static_cast<Role::Type>(temp);
    out = role >= r;
  } else {
    out = false;
  }
  return Code::Okay; 
}
